<!DOCTYPE html>
<html lang="zh-Hans">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="PHP-Redis学习实例"/>




  <meta name="keywords" content="技术, PHP, Redis, 杂鱼君的博客" />










  <link rel="alternate" href="/atom.xml" title="杂鱼君的博客">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="http://notes.wen.moe/post/learn-php-redis.html"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>









    <title> PHP-Redis学习实例 - 杂鱼君的博客 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">杂鱼君的博客</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            Home
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            Archives
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            Tags
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            About
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">杂鱼君的博客</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              Home
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              Archives
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              Tags
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              About
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          PHP-Redis学习实例
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2014-09-22
        </span>
        
          <div class="post-category">
            
              <a href="/categories/技术/">技术</a>
            
          </div>
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">Contents</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#lset-lget-通过下标设置-获取元素值"><span class="toc-text">lset / lget 通过下标设置/获取元素值</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Set类型"><span class="toc-text">Set类型:</span></a>
    </div>
  </div>


    <div class="post-content">
      
        <p>Redis是一个和Memcache类似的NoSQL缓存系统.相比Memcache支持更多的数据类型,并且数据可以持久化,这让他可以储存超过指定内存大小的数据(这是我觉得和Memcache最大的区别).</p>
<p>由于支持的数据格式多,所以操作相比Memcache稍微复杂些.</p>
<p>Redis的各种赋值和取值的操作很多,网上想找个齐全的资料都不太好找.以下整理记录了一些自己常用的操作.</p>
<a id="more"></a>
<p>##String类型</p>
<p>####set / mset / setnx / msetnx 赋值操作</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();<span class="comment">//清空所有的key</span></div><div class="line">var_dump($redis-&gt;set(<span class="string">'key'</span>,<span class="string">'a'</span>));<span class="comment">//设置一个key-value,如果已存在key,就覆盖</span></div><div class="line">var_dump($redis-&gt;mset(<span class="keyword">array</span>(<span class="string">'key1'</span>=&gt;<span class="number">1</span>,<span class="string">'key2'</span>=&gt;<span class="number">2</span>))); <span class="comment">//同时设置多个key-value,如果已存在key,就覆盖.并且这个操作是原子性的,要么都成功,要么都失败</span></div><div class="line">var_dump($redis-&gt;setnx(<span class="string">'key'</span>,<span class="string">'a'</span>))<span class="comment">//设置一个key-value,如果已存在key,返回false表示失败</span></div><div class="line">var_dump($redis-&gt;msetnx(<span class="keyword">array</span>(<span class="string">'key1'</span>=&gt;<span class="number">1</span>,<span class="string">'key2'</span>=&gt;<span class="number">2</span>)));<span class="comment">//设置多个key-value,如果其中有一个已存在key,则全部失败</span></div></pre></td></tr></table></figure>
<p>####get / mget 取值操作</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;get(<span class="string">'key'</span>)); <span class="comment">//获取一个key,如果不存在,返回false</span></div><div class="line">var_dump($redis-&gt;mget(<span class="keyword">array</span>(<span class="string">'key1'</span>=&gt;<span class="number">1</span>,<span class="string">'key2'</span>=&gt;<span class="number">2</span>))); <span class="comment">//同时获取多个key,以数组形式返回,不存在的key返回false</span></div></pre></td></tr></table></figure>
<p>####incr / decr 自增/自减赋值</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();<span class="comment">//清空所有的key</span></div><div class="line">$redis-&gt;set(<span class="string">'key'</span>,<span class="number">10</span>);</div><div class="line">var_dump($redis-&gt;incr(<span class="string">'key'</span>)); <span class="comment">//结果:int(11)</span></div><div class="line">var_dump($redis-&gt;decr(<span class="string">'key'</span>)); <span class="comment">//结果:int(11)</span></div><div class="line"><span class="comment">//如果key是个字符串:</span></div><div class="line">$redis-&gt;set(<span class="string">'key'</span>,<span class="string">'aa'</span>);</div><div class="line">var_dump($redis-&gt;incr(<span class="string">'key'</span>)); <span class="comment">//结果:bool(false)</span></div><div class="line">var_dump($redis-&gt;decr(<span class="string">'key'</span>)); <span class="comment">//结果:bool(false)</span></div></pre></td></tr></table></figure>
<p>##List类型</p>
<p>####lpush /rpush 列表头部/尾部添加一个元素<br><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;lpush(<span class="string">'key'</span>,<span class="string">'a'</span>)); <span class="comment">//结果:int(1)返回列表长度</span></div><div class="line">var_dump($redis-&gt;lpush(<span class="string">'key'</span>,<span class="string">'b'</span>)); <span class="comment">//结果:int(2) 返回列表长度</span></div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'c'</span>)); <span class="comment">//结果:int(3)返回列表长度</span></div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'d'</span>)); <span class="comment">//结果:int(4) 返回列表长度</span></div></pre></td></tr></table></figure></p>
<h4 id="lset-lget-通过下标设置-获取元素值"><a href="#lset-lget-通过下标设置-获取元素值" class="headerlink" title="lset / lget 通过下标设置/获取元素值"></a>lset / lget 通过下标设置/获取元素值</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'a'</span>));<span class="comment">//设置第1个值为a</span></div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'b'</span>)); <span class="comment">//设置第2个值为b</span></div><div class="line">var_dump($redis-&gt;lget(<span class="string">'key'</span>,<span class="number">1</span>)); <span class="comment">//下标1的值是 "b"</span></div><div class="line">var_dump($redis-&gt;lset(<span class="string">'key'</span>,<span class="number">1</span>,<span class="string">'c'</span>)); <span class="comment">//设置第1个值为c</span></div><div class="line">var_dump($redis-&gt;lget(<span class="string">'key'</span>,<span class="number">1</span>)); <span class="comment">//下标1的值是 "c"</span></div></pre></td></tr></table></figure>
<p>####llen返回list的长度</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'a'</span>)); </div><div class="line">var_dump($redis-&gt;llen(<span class="string">'key'</span>)); <span class="comment">//结果:int(1) 如果key不是list类型,返回false</span></div></pre></td></tr></table></figure>
<p>####lrange返回指定区间内的元素</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'a'</span>)); </div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'b'</span>)); </div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'c'</span>)); </div><div class="line">var_dump($redis-&gt;lrange(<span class="string">'key'</span>,<span class="number">0</span>,<span class="number">1</span>)); <span class="comment">//结果array('a','b');</span></div></pre></td></tr></table></figure>
<p>####lpop / rpop 从头部/尾部弹出一个元素</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'b'</span>)); </div><div class="line">var_dump($redis-&gt;rpush(<span class="string">'key'</span>,<span class="string">'c'</span>)); </div><div class="line">var_dump($redis-&gt;lpop(<span class="string">'key'</span>)); string(<span class="number">1</span>) <span class="string">"a"</span></div><div class="line">var_dump($redis-&gt;rpop(<span class="string">'key'</span>)); <span class="comment">//string(1) "c"</span></div></pre></td></tr></table></figure>
<h2 id="Set类型"><a href="#Set类型" class="headerlink" title="Set类型:"></a>Set类型:</h2><p><em>set是一种无序集合,用来记录不重复的数据</em></p>
<p>####sadd 添加一个元素到集合中</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'a'</span>));<span class="comment">//返回int(1)</span></div></pre></td></tr></table></figure>
<p>####smembers 返回set中所有元素(无序)<br><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;smembers(<span class="string">'key'</span>));<span class="comment">//array(2) &#123; [0]=&gt; string(1) "a" [1]=&gt; string(1) "b" &#125;</span></div></pre></td></tr></table></figure></p>
<p>####scard 获取set中元素个数</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;scard(<span class="string">'key'</span>));<span class="comment">//返回int(2)</span></div></pre></td></tr></table></figure>
<p>####srem 删除指定的元素</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;srem(<span class="string">'key'</span>,<span class="string">'b'</span>));<span class="comment">//删除成功,返回int(1)</span></div></pre></td></tr></table></figure>
<p>####spop 随机弹出set中一个元素</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;spop(<span class="string">'key'</span>));<span class="comment">//string(b)</span></div><div class="line">var_dump($redis-&gt;scard(<span class="string">'key'</span>));<span class="comment">//返回int(1)</span></div></pre></td></tr></table></figure>
<p>####spop 随机获取set中一个元素,不删除</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;srandmember(<span class="string">'key'</span>));<span class="comment">//string(b)</span></div><div class="line">var_dump($redis-&gt;scard(<span class="string">'key'</span>));<span class="comment">//返回int(2)</span></div></pre></td></tr></table></figure>
<p>####sismember 判断set中是否有某个元素</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;sismember(<span class="string">'key'</span>,<span class="string">'a'</span>));<span class="comment">//bool(true)</span></div><div class="line">var_dump($redis-&gt;sismember(<span class="string">'key'</span>,<span class="string">'a'</span>));<span class="comment">//bool(false)</span></div></pre></td></tr></table></figure>
<p>####sinter sinterstore 返回给定key的交集</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key1'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key1'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key2'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key2'</span>,<span class="string">'c'</span>));</div><div class="line">var_dump($redis-&gt;sinter(<span class="string">'key1'</span>,<span class="string">'key2'</span>));<span class="comment">//结果:array(1) &#123; [0]=&gt; string(1) "a" &#125;</span></div><div class="line">var_dump($redis-&gt;sinterstore(<span class="string">'key3'</span>,<span class="string">'key1'</span>,<span class="string">'key2'</span>));<span class="comment">//两个集合的交集存入key3,该函数返回int(1)</span></div><div class="line">var_dump($redis-&gt;smembers(<span class="string">'key3'</span>));<span class="comment">//结果:array('a')</span></div></pre></td></tr></table></figure>
<p>####sunion sunionstore 返回给定key的并集</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key1'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key1'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key2'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key2'</span>,<span class="string">'c'</span>));</div><div class="line">var_dump($redis-&gt;sunion(<span class="string">'key1'</span>,<span class="string">'key2'</span>));<span class="comment">//结果:array('c','a','b')</span></div><div class="line">var_dump($redis-&gt;sunionstore(<span class="string">'key3'</span>,<span class="string">'key1'</span>,<span class="string">'key2'</span>));</div><div class="line">var_dump($redis-&gt;smembers(<span class="string">'key3'</span>));<span class="comment">//结果:array('c','a','b')</span></div></pre></td></tr></table></figure>
<p>####sdiff sdiffstore 返回给定key的差集</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">$redis = <span class="keyword">new</span> redis();  </div><div class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);  </div><div class="line">$redis-&gt;flushall();</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key1'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key1'</span>,<span class="string">'b'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key2'</span>,<span class="string">'a'</span>));</div><div class="line">var_dump($redis-&gt;sadd(<span class="string">'key2'</span>,<span class="string">'c'</span>));</div><div class="line">var_dump($redis-&gt;sdiff(<span class="string">'key1'</span>,<span class="string">'key2'</span>));<span class="comment">//结果:array('b')</span></div><div class="line">var_dump($redis-&gt;sdiffstore(<span class="string">'key3'</span>,<span class="string">'key1'</span>,<span class="string">'key2'</span>));<span class="comment">//两个集合的交集存入key3,该函数返回int(1)</span></div><div class="line">var_dump($redis-&gt;smembers(<span class="string">'key3'</span>));<span class="comment">//结果:array('b')</span></div></pre></td></tr></table></figure>
<p>以上是三种Redis常用的数据类型,其他的比如Hash什么的下次再补上.Redis有一个比较让PHPer不适应的地方是,他的底层是强类型的,不同类型数据不能覆盖.比如:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">var_dump($redis-&gt;set(<span class="string">"key1"</span>,<span class="string">'a'</span>));   <span class="comment">//结果：bool(true) </span></div><div class="line">var_dump($redis-&gt;sadd(<span class="string">"key1"</span>,<span class="string">'b'</span>));   <span class="comment">//结果：bool(false) 无法赋值</span></div></pre></td></tr></table></figure>
      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>Author: </span>
      <a href="http://notes.wen.moe">zakokun</a>
    </p>
    <p class="copyright-item">
      <span>Link: </span>
      <a href="http://notes.wen.moe/post/learn-php-redis.html">http://notes.wen.moe/post/learn-php-redis.html</a>
    </p>
    <p class="copyright-item">
      <span>License: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/技术/">技术</a>
            
              <a href="/tags/PHP/">PHP</a>
            
              <a href="/tags/Redis/">Redis</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/post/php-method-overload.html">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">手动实现PHP的方法重载</span>
        <span class="prev-text nav-mobile">Prev</span>
      </a>
    
    
      <a class="next" href="/post/how-to-export-csv-file-by-php.html">
        <span class="next-text nav-default">php导出csv文件的一些操作</span>
        <span class="prev-text nav-mobile">Next</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:me@wen.moe" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://www.instagram.com/zakokun" class="iconfont icon-instagram" title="instagram"></a>
        
      
    
    
    
  </div>


<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://hexo.io/">Hexo</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Theme -
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Hosted by <a class="theme-link" href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
  </span>

  <span class="copyright-year">
    
    &copy;
    
      2014 -
    
    2017

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">zakokun</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'http://notes.wen.moe/post/learn-php-redis.html';
        this.page.identifier = 'post/learn-php-redis.html';
        this.page.title = 'PHP-Redis学习实例';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//evanszhang.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>




    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>

  </body>
</html>
